﻿using blackhouse.WebNemo.Business;
using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.WebNemo.Web.Nemo.Manager {
    public partial class GroupColumns : System.Web.UI.Page {

        #region Fields

        private Factory factory = Factory.Instance();
        private IGroup group;
        private NemoUrl stateUrl;

        #endregion

        #region Methods

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.SetScenario();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.PopulateView();
        }

        private void SetScenario() {
            this.stateUrl = new NemoUrl();
            if (this.stateUrl.GroupId > 0) {
                this.group = new Group(this.stateUrl.GroupId);
            }
        }

        private void PopulateView() {
            this.ltlHeaderName.Text = String.Format("Kolumny {2} „{0}” ({1})", this.group.Name, this.group.SystemName, (this.group.Type == GroupType.Table) ? "grupy" : "widoku");
            this.Title = this.ltlHeaderName.Text;

            this.nemoTabNemo.Visible = true;

            NemoUrl editGroupUrl = new NemoUrl(NemoUrlView.EditGroup);
            editGroupUrl.GroupId = this.group.Id;
            editGroupUrl.GroupType = this.group.Type;
            editGroupUrl.MissingInReturnHistory = true;
            this.nemoTabNemo.AddLinkMenuItem("Ogólne", editGroupUrl.GetUrl());

            this.nemoTabNemo.AddLinkMenuItem("Kolumny", String.Empty, true);
            this.nemoTabNemo.AddLinkMenuItem("Generator XML", "#xml");

            this.rptColumns.DataSource = this.group.Columns.OrderBy(gc => gc.ColumnName);
            this.rptColumns.DataBind();

            this.hiddenSortGroup.Value = "columns-" + this.group.Id;

            NemoJsLibrary njq = new NemoJsLibrary();
            njq.IncludeSorter();
        }

        protected void ItemDataBound_rptColumns(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:
                    GroupColumn gc = e.Item.DataItem as GroupColumn;
                    if (gc != null) {
                        Literal ltlGroupType = e.Item.FindControl("ltlGroupType") as Literal;
                        HyperLink lnkRegex = e.Item.FindControl("lnkRegex") as HyperLink;
                        HyperLink lnkFormat = e.Item.FindControl("lnkFormat") as HyperLink;
                        HyperLink lnkEditControl = e.Item.FindControl("lnkEditControl") as HyperLink;
                        HyperLink lnkEditable = e.Item.FindControl("lnkEditable") as HyperLink;

                        if (ltlGroupType != null) {
                            switch (gc.ColumnType) {
                                case FieldType.ValueField:
                                    ltlGroupType.Text = "Wartość: " + gc.DataType.ToDbType();
                                    break;
                                case FieldType.OneReference:
                                    ltlGroupType.Text = "Referencja 1 do 1 (->" + factory.GetGroup(gc.RelatedGroupId).Name + ")";
                                    break;
                                case FieldType.ManyReference:
                                    ltlGroupType.Text = "Referencja 1 do wielu (->" + factory.GetGroup(gc.RelatedGroupId).Name + ")";
                                    break;
                                case FieldType.FileField:
                                    ltlGroupType.Text = "Plik";
                                    break;
                                case FieldType.DateField:
                                    ltlGroupType.Text = "Data";
                                    break;
                                case FieldType.DateTimeField:
                                    ltlGroupType.Text = "Data z czasem";
                                    break;
                                default:
                                    break;
                            }
                        }

                        if (lnkRegex != null) {
                            if (String.IsNullOrEmpty(gc.RegexValidation))
                                lnkRegex.Visible = false;
                            else {
                                lnkRegex.Text = "Walidacja regex: " + gc.RegexValidation;
                                lnkRegex.ToolTip = lnkRegex.Text.ReplaceEx("\"", "''");
                            }
                        }

                        if (lnkFormat != null) {
                            if (String.IsNullOrEmpty(gc.FormattingValue) || gc.FormattingValue == "{0}")
                                lnkFormat.Visible = false;
                            else {
                                lnkFormat.Text = "Formatowanie: " + gc.FormattingValue;
                                lnkFormat.ToolTip = lnkFormat.Text.ReplaceEx("\"", "''");
                            }
                        }

                        if (lnkEditControl != null) {
                            if (String.IsNullOrEmpty(gc.CustomEditControl))
                                lnkEditControl.Visible = false;
                            else {
                                lnkEditControl.Text = "Własna kontrolka edycyjna: " + gc.CustomEditControl;
                                lnkEditControl.ToolTip = lnkEditControl.Text.ReplaceEx("\"", "''");
                            }
                        }

                        if (lnkEditable != null) {
                            if (gc.HideForEdition || !gc.AllowUpdateValue)
                                lnkEditable.Visible = false;
                        }

                    }
                    break;
            }
        }

        #endregion

    }
}